package com.mes.loco.aps.server.controller.wms;

import com.iplant.aps.controller.BaseController;
import com.iplant.aps.service.WMSService;
import com.iplant.aps.service.po.ServiceResult;
import com.iplant.aps.service.po.bms.BMSEmployee;
import com.iplant.aps.service.po.wms.WMSPickDemandItemStock;
import com.iplant.aps.service.utils.StringUtils;
import com.iplant.aps.utils.RetCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author PengYouWang
 * 2022-7-4 14:46:29
 */
@RestController
@RequestMapping("/api/WMSPickDemandItemStock")
public class WMSPickDemandItemStockController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(WMSPickDemandItemStockController.class);

    final
    WMSService wWMSService;

    public WMSPickDemandItemStockController(WMSService wWMSService) {
        this.wWMSService = wWMSService;
    }

    /**
     * 条件查询
     */
    @GetMapping("/All")
    public Object All(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<>();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            int wTaskID = StringUtils.parseInt(request.getParameter("TaskID"));
            String wBatchNumber = StringUtils.parseString(request.getParameter("BatchNumber"));
            String wSerialNo = StringUtils.parseString(request.getParameter("SerialNo"));

            ServiceResult<List<WMSPickDemandItemStock>> wServiceResult =
                    wWMSService.WMS_QueryPickDemandItemStockList(wLoginUser,
                            wTaskID, wBatchNumber, wSerialNo);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
                for (String wKey : wServiceResult.CustomResult.keySet()) {
                    this.SetResult(wResult, wKey, wServiceResult.CustomResult.get(wKey));
                }
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 通过配送单明细ID集合查询库存集合
     */
    @GetMapping("/QueryAllByTaskIDList")
    public Object QueryAllByTaskIDList(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            String wTaskIDs = StringUtils.parseString(request.getParameter("TaskIDs"));

            if (StringUtils.isEmpty(wTaskIDs)) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<List<WMSPickDemandItemStock>> wServiceResult =
                    wWMSService.WMS_QueryQueryAllByTaskIDList(wLoginUser, wTaskIDs);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }
}
